//
//  YWAssetCheckManager.m
//  maintain
//
//  Created by 烟雨云渊 on 16/10/27.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWAssetCheckManager.h"
#import "YWAssetCheckDBManager.h"
#import "WMHttpTool.h"
#import "YWCommonTool.h"
#import "YWMacro.h"
#import "YWConst.h"
#import "MJExtension.h"

#import "YWAssetCheckRecordDetails.h"
#import "YWAssetCheckRecord.h"
#import "YWLocation.h"
#import "YWBuilding.h"
#import "YWApartment.h"
#import "YWFloor.h"
#import "YWEquipmentType.h"
#import "YWReply.h"
#import "YWHandler.h"
#import "YWLoginTool.h"
#import "YWReplyManager.h"

@implementation YWAssetCheckManager
YWSingletonM(Manager)
#pragma mark - 资产清点表的更新、取值
- (BOOL)updateAssetCheckWithArray:(NSArray*)checkArray
{
    return [[YWAssetCheckDBManager shareManager] updateAssetCheckWithArray:checkArray];
}
- (NSArray*)assetCheckWithPage:(NSInteger)page locationId:(NSInteger)locationId
{
    return [[YWAssetCheckDBManager shareManager] assetCheckWithPage:page locationId:locationId];
}
- (YWAssetCheckRecord*)assetCheckRecordWithCheckRecordId:(NSInteger)checkRecordId
{
    return [[YWAssetCheckDBManager shareManager] assetCheckRecordWithCheckRecordId:checkRecordId];
}
#pragma mark - 资产清点明细表的更新、取值
- (BOOL)updateAssetCheckDetailWithArray:(NSArray*)checkDetailArray
{
    return [[YWAssetCheckDBManager shareManager] updateAssetCheckDetailWithArray:checkDetailArray];
}
#pragma mark - 有关assetCheck的网络请求

- (void)requestAssetCheckRecordWithPage:(NSInteger)page pageSize:(NSInteger)pageSize locationId:(NSInteger)locationId WithComplete:(void (^)(BOOL))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (page) {
        [params setObject:@(page) forKey:@"page"];
    }
    if (pageSize) {
        [params setObject:@(pageSize) forKey:@"pageSize"];
    }
    if (locationId) {
        [params setObject:@(locationId) forKey:@"locationId"];
    }
    NSString * url = [YWCommonTool assetCheckRecordUrl];
    YWLog(@"url:%@,param =%@",url,params);
    [[WMHttpTool shareHttpTool] getWithUrl:url params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray * assetCheckArray = [YWAssetCheckRecord mj_objectArrayWithKeyValuesArray:json];
        
        [[YWAssetCheckManager shareManager] updateAssetCheckWithArray:assetCheckArray];
        
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"请求失败,稍后请重试"];
        }
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)postAssetCheckRecordWithCheckRecord:(YWAssetCheckRecord*)checkRecord recordDetails:(NSMutableArray*)recordDetails WithComplete:(void (^)(BOOL))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setValue:checkRecord.building.buildingId?:@"" forKey:@"building.buildingId"];
    [params setValue:@(checkRecord.apartment.apartmentId)?:@"" forKey:@"apartment.apartmentId"];
    [params setValue:@(checkRecord.floor.floorId)?:@"" forKey:@"floor.floorId"];
    [params setValue:@(checkRecord.location.locationId)?:@"" forKey:@"location.locationId"];
    [params setValue:checkRecord.note?:@"" forKey:@"note"];
    [recordDetails enumerateObjectsUsingBlock:^(YWAssetCheckRecordDetails * _Nonnull recordDetail, NSUInteger idx, BOOL * _Nonnull stop) {
        [params setValue:@(recordDetail.equipmentType.equipmentTypeId)?:@"" forKey:[NSString stringWithFormat:@"recordDetails[%lu].equipmentType.equipmentTypeId",(unsigned long)idx]];
        [params setValue:@(recordDetail.checkQuantity)?:@"" forKey:[NSString stringWithFormat:@"recordDetails[%lu].checkQuantity",(unsigned long)idx]];
    }];
    
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] postWithUrl:[YWCommonTool assetCheckRecordUrl] params:params success:^(NSHTTPURLResponse *response, id json) {
        YWAssetCheckRecord *assetCheckRecord = [YWAssetCheckRecord mj_objectWithKeyValues:json];
        [[YWAssetCheckManager shareManager] updateAssetCheckWithArray:@[assetCheckRecord]];
        
        YWLog(@"%ld",(long)response.statusCode);
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"提交失败,稍后请重试"];
        }
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)postCheckRecordReplyWithReply:(YWReply*)reply atUser:(YWHandler*)atUser WithComplete:(void (^)(BOOL))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setValue:reply.content?:@"" forKey:@"content"];
    [params setObject:@(atUser.uniqueId) forKey:[NSString stringWithFormat:@"atUser.uniqueId"]];
    [params setValue:@(reply.relationId)?:@"" forKey:@"relationId"];
    [params setValue:@(reply.relationType)?:@"" forKey:@"type"];
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] postWithUrl:[YWCommonTool postReplyUrl] params:params success:^(NSHTTPURLResponse *response, id json) {
        YWLog(@"json = %@",json);
        reply.replyId = [[json objectForKey:@"replyId"] integerValue];
        reply.replyTime = [[json objectForKey:@"replyTime"] longLongValue];
        reply.atUser = atUser;
        
        YWHandler *replier = [[YWHandler alloc] init];
        replier.uniqueId = [YWLoginTool loginUser].uniqueId;
        reply.replier = replier;
        reply.status = 0;
        reply.isRead = YES;
        
        [[YWReplyManager shareManager] updateReplyWithReplyArray:@[reply]];
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"提交失败,稍后请重试"];
        }
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

@end
